<?php
/**
 * Create By: PhpStorm
 * User: niechenguang
 * File: GetCircle.php
 * Date: 2017/1/2
 * Time: 23:13
 */

namespace app\api\model\page\chat;

use app\api\model\dao\UserAccount;
use app\library\Common;
use app\library\Error;
use app\library\HuanXin;
use app\library\PageBase;
use think\Log;

class DelFriend extends PageBase
{

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * page 入口
     *
     * @param $param
     * @return array
     */
    public function excute($param)
    {
        $ret = $this->checkToken($param);
        if (false === $ret) {
            return $this->getRes(Error::ERR_NO_AUTH);
        }

        $this->huanxin_username = Common::encryptString($this->phone);

        if (empty($param['userid'])) {
            return $this->getRes(Error::ERR_PARAM);
        }

        $param['userid'] = intval($param['userid']);

        $objAccount = new UserAccount();
        $user_account = $objAccount->getUserAccount(['user_id' => $param['userid']]);
        if (false === $user_account) {
            return $this->getRes(Error::ERR_SYS);
        }
        if (empty($user_account[0])) {
            return $this->getRes(Error::ERR_USER_NOT_EXIST);
        }
        $user_account = $user_account[0];

        $ret = HuanXin::delFriend($this->huanxin_username, $user_account['huanxin_username']);
        if (!empty($ret['error'])) {
            Log::error(__METHOD__ . ' del friend fail.[ret:' . json_encode($ret) . '][user_name:' . $this->huanxin_username . '][friend_name:' . $user_account['huanxin_username'] . ']');
            return $this->getRes(Error::ERR_SYS);
        }

        return $this->getRes();

    }
}
